import { t } from 'i18next'
import { fetchStatus } from '@/api/global'
import Alert from '@/components/Alert'
import { useGlobalStore } from '@/store/global'
import { StyledMediaContainer } from '@/style/global'
import jumpPage from '@/utils/jumpPage'
import { Button, Tab } from '@kedacom/react-next'
import { StyledMenu } from '../style'
import shallow from 'zustand/shallow'
import { checkPermission } from '@/utils'

interface MenuTabProps {
	value: number
	isShowCreateBtn?: boolean
}
/*
tab组件

export function checkPermission(userPermissions: string, permission: string) {
	//ie 不兼容直接 0bxxx
	return (Number('0b' + userPermissions) & Number('0b' + permission)) === Number('0b' + userPermissions)
}

*/
export default function MenuTab({ value, isShowCreateBtn = true }: MenuTabProps) {
	const { user, SWITCH } = useGlobalStore((state) => ({ user: state.user, SWITCH: state.SWITCH }), shallow)
	let data = [
		{
			label: t('会议日程'),
			value: 1,
			hidden: !checkPermission('10000000', SWITCH.homeTab),
		},
		{
			label: t('会场管理'),
			value: 2,
			hidden: !checkPermission('01000000', SWITCH.homeTab),
		},
		{
			label: t('虚拟会议室'),
			value: 3,
			hidden: !user?.enable_meeting || !checkPermission('00100000', SWITCH.homeTab),
		},
		{
			label: t('会议模板'),
			value: 4,
			hidden: !user?.enable_meeting || !checkPermission('00010000', SWITCH.homeTab),
		},
		{
			label: t('个性化设置'),
			value: 5,
			hidden: !user?.enable_meeting || !checkPermission('00001000', SWITCH.homeTab),
		},
		{
			label: t('会议审批'),
			value: 6,
			hidden: !user?.enable_meeting || !user?.enable_cms_approval || !checkPermission('00000100', SWITCH.homeTab),
		},
		{
			label: t('系统日志'),
			value: 7,
			hidden: !user?.enable_meeting || !user?.is_meeting_admin || !checkPermission('00000010', SWITCH.homeTab),
		},
		{
			label: t('系统配置'),
			value: 8,
			hidden: !user?.enable_meeting || !user?.is_meeting_admin || !checkPermission('00000001', SWITCH.homeTab),
		},
	]
	const handleChange = (val: number) => {
		switch (val) {
			case 1:
				jumpPage('meetingList')
				break
			case 2:
				jumpPage('index')
				break
			case 3:
				jumpPage('findVirtualRoom')
				break
			case 4:
				jumpPage('template')
				break
			case 5:
				jumpPage('personalConfig')
				break
			case 6:
				jumpPage('auditManage')
				break
			case 7:
				jumpPage('sysLog')
				break
			case 8:
				jumpPage('sysConfig')
				break
		}
	}
	const createMeeting = () => {
		if (!user?.machine_room_moid) {
			Alert(t('无会议资源，请联系管理员！'))
			return
		}
		/*
		# callback作为参数
		export const fetchStatus = async (callback: Function) =>
			requestMeeting<{ independ: number; describe?: string }>({
				url: '/independ_status',
				method: 'get',
			}).then((res) => {
				if (res.success) {
					if (res.data?.independ) {
						Alert(res.data.describe || t('当前机房为托管状态，无法操作！'))
					} else {
						callback()
					}
				} else {
					Alert(t('当前机房为托管状态，无法操作！'))
				}
			})

		*/
		fetchStatus(() => {
			jumpPage('create')
		})
	}
	return (
		<StyledMediaContainer>
			<StyledMenu className="meeting-tab">
				<Tab data={data.filter((i) => !i.hidden)} value={value} onChange={(e, v) => handleChange(v)}></Tab>
				{user?.enable_meeting === 1 && isShowCreateBtn && (
					<Button className="create-meeting-btn" type="primary" onClick={createMeeting}>
						{t('创建会议')}
					</Button>
				)}
			</StyledMenu>
		</StyledMediaContainer>
	)
}
